package com.zhiyou100.taxi.acction;

import com.github.pagehelper.PageInfo;
import com.zhiyou100.taxi.entity.CarCheck;
import com.zhiyou100.taxi.service.CarCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashSet;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-27 15:27
 * @Description
 */
@Controller
@RequestMapping("/carCheck")
public class CarCheckController {
    @Autowired
    CarCheckService carCheckService;

    @RequestMapping("/show.action")
    public String show() {
        return "forward:/carCheck/page.action";
    }

    @RequestMapping("/page.action")
    public String page(Integer pageNum, HttpServletRequest request) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<CarCheck> carCheckPageInfo = carCheckService.getCarPage(pageNum);
        request.getSession().setAttribute("carCheckPageInfo", carCheckPageInfo);
        return "busines/caCheckList";
    }

    @RequestMapping("/deleteBatch.action")
    public String deleteBatch(String carCheckIds, HttpServletRequest request) {
        String[] split = carCheckIds.split(",");
        HashSet<String> set = new HashSet<>(Arrays.asList(split));
        int num = carCheckService.deleteByPrimaryKeys(set);
        if (num > 0) {
            request.setAttribute("msg", "删除成功");
        } else {
            request.setAttribute("msg", "删除失败");
        }
        return "forward:/carCheck/page.action";
    }

    @RequestMapping("/getOne.action")
    public String getOne(String carCheckId, HttpServletRequest request) {
        CarCheck carCheck = carCheckService.getOneByPrimaryKey(carCheckId);
        request.setAttribute("carCheck", carCheck);
        return "busines/car-check-edit";
    }

    @RequestMapping("/save.action")
    public String save(CarCheck carCheck, HttpServletRequest request) {
        carCheck.setCarCheckId(carCheckService.getPrimaryKey());
        int num = carCheckService.insert(carCheck);
        if (num > 0) {
            request.setAttribute("msg", "保存成功");
        } else {
            request.setAttribute("msg", "保存失败");
        }
        return "forward:/carCheck/page.action";
    }

    @RequestMapping("/update.action")
    public String update(CarCheck carCheck, HttpServletRequest request) {
        int num = carCheckService.updateByPrimaryKey(carCheck);
        if (num > 0) {
            request.setAttribute("msg", "修改成功");
        } else {
            request.setAttribute("msg", "修改失败");
        }
        return "forward:/carCheck/page.action";
    }

}
